package com.olinsports.system.olinsportssystem.service;

import com.olinsports.system.olinsportssystem.model.*;
import com.olinsports.system.olinsportssystem.model.InventoryDetails;
import com.olinsports.system.olinsportssystem.repository.InventoryDetailsRepository;
import com.olinsports.system.olinsportssystem.repository.InventoryDetailsRepository;
import com.olinsports.system.olinsportssystem.util.PageUtil;
import com.olinsports.system.olinsportssystem.util.ViewExcel;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.List;
import java.util.*;

/**
 * Created by Administrator on 2017/7/22.
 */
@Service
public class InventoryDetailsService {
    @Autowired
    private InventoryDetailsRepository inventoryDetailsRepository;
    @Autowired
    private CommodityTypeService commodityTypeService;
    @Autowired
    private CommodityDetailedService commodityDetailedService;
    private int totalPages = 0;//总页数

    private int  totalRows;//总数据量
    @Autowired
    private PageUtil pageUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;


    public List<InventoryDetails> selectAll(){
        return inventoryDetailsRepository.selectAll();
    }
    public int insertAll(InventoryDetails InventoryDetails){
        return inventoryDetailsRepository.insertAll(InventoryDetails);
    }
    public int deleteByKey(int detailedKey){
        return inventoryDetailsRepository.deleteByKey(detailedKey);
    }

    public int updateData(InventoryDetails InventoryDetails){
        return inventoryDetailsRepository.updateData(InventoryDetails);
    }

    public InventoryDetails selectByKey(int inventoryKey){
        return inventoryDetailsRepository.selectByKey(inventoryKey);
    }

    public Map listOrSearchInvent(String page, String rows, boolean _search, String sord, String sidx){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        if(_search){//进行查找操作
            List<MultiCondition> multiList = pageUtil.searchList(page,rows,_search,request);

            //通过条件进行查询总数据量
            totalRows =  inventoryDetailsRepository.findByParamsCount(multiList);

            System.out.println("totalRows = "+totalRows);
            //通过条件查询beg-rows的数据，分页查询
            List<InventoryDetails> stmCountSearch = inventoryDetailsRepository.findByParamsLimit(sord,sidx,multiList,beg,Integer.valueOf(rows));
            Map mapSearch =pageUtil.paging(rows,page,totalRows);
            //设置表格数据
            mapSearch.put("rows",stmCountSearch);
            return mapSearch;
        } else {
            //查询当前表中的总数据量
            totalRows = inventoryDetailsRepository.findCount();
            System.out.println("totalRows = "+totalRows);
            //通过分页进行查询当前表中的数据
            List<InventoryDetails> stmCountList =  inventoryDetailsRepository.findLimit(sord,sidx,beg,Integer.valueOf(rows));

            Map mapList = pageUtil.paging(rows,page,totalRows);
            mapList.put("rows",stmCountList);//设置行显示的数据

            return mapList;
        }
    }

    public int saveInventoryDetails(InventoryDetails InventoryDetails,int typekey,int detailsKey) {
        CommodityType commodityType = commodityTypeService.selectByTypeKey(typekey);
        Date date = new Date(System.currentTimeMillis());//获取当前时间

        StmUsr stmUsr = (StmUsr)request.getSession().getAttribute("stmUsr");
        System.out.println("InventoryDetails = "+InventoryDetails.getInventoryKey());

        CommodityDetailed commodityDetailed = new CommodityDetailed();
        commodityDetailed.setAmdLog(date);
        commodityDetailed.setStock(InventoryDetails.getStock());
        commodityDetailed.setDetailedKey(detailsKey);

        commodityDetailedService.updateData(commodityDetailed);

        InventoryDetails.setAmdLog(date);
        InventoryDetails.setDate(date);
        InventoryDetails.setSkuType(commodityType);
        InventoryDetails.setOperatorNum(stmUsr);
        InventoryDetails.setAmdUsr(stmUsr);

       return inventoryDetailsRepository.insertAll(InventoryDetails);
    }

    public Map searchDate(String page, String rows,String startDate,String endDate){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = inventoryDetailsRepository.selectByDateCount(startDate,endDate);
        List<InventoryDetails> stmCountList =  inventoryDetailsRepository.selectByDateLimit(startDate,endDate,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",stmCountList);//设置行显示的数据
        return mapList;
    }

    public  List<InventoryDetails>  toExcelHttp(String startDate,String endDate){
        List<InventoryDetails>  inventoryDetailss = inventoryDetailsRepository.selectByDateAll(startDate,endDate);
        return inventoryDetailss;
    }

    public ModelAndView toDcExcel(String startDate, String endDate, HSSFWorkbook workbook) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        List<InventoryDetails> inventoryDetailss = inventoryDetailsRepository.selectByDateAll(startDate,endDate);
        map.put("inventoryDetailss",inventoryDetailss);
        ViewExcel viewExcel = new ViewExcel("出入库明细","出入库明细");
        viewExcel.buildExcelDocument(map, workbook, request, response);
        return new ModelAndView(viewExcel, map);
    }

    public Map searchSelf(String page, String rows,String field,String fieldValue){
        int beg = (Integer.valueOf(page)-1)*Integer.valueOf(rows);
        totalRows = inventoryDetailsRepository.selectSelfCount(field,fieldValue);
        List<InventoryDetails> stmCountList =  inventoryDetailsRepository.selectSelfLimit(field,fieldValue,beg,Integer.valueOf(rows));
        Map mapList = pageUtil.paging(rows,page,totalRows);
        mapList.put("rows",stmCountList);//设置行显示的数据
        return mapList;
    }
}
